{% extends 'base.html' %}
{% load static %}

{% block title %}技术人员仪表板 - 维修管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <h1 class="mb-4">技术人员仪表板</h1>
            <p class="text-muted">欢迎回来，{{ user.get_full_name|default:user.username }}！</p>
        </div>
    </div>

    <!-- 工单统计 -->
    <div class="row mb-4">
        <div class="col-md-6 col-lg-3 mb-3">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <h5 class="card-title">总工单</h5>
                    <h2 class="card-text">{{ ticket_stats.total }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-lg-3 mb-3">
            <div class="card bg-warning text-dark">
                <div class="card-body">
                    <h5 class="card-title">维修中</h5>
                    <h2 class="card-text">{{ ticket_stats.repairing }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-lg-3 mb-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <h5 class="card-title">质检中</h5>
                    <h2 class="card-text">{{ ticket_stats.quality_check }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-lg-3 mb-3">
            <div class="card bg-success text-white">
                <div class="card-body">
                    <h5 class="card-title">已完成</h5>
                    <h2 class="card-text">{{ ticket_stats.completed }}</h2>
                </div>
            </div>
        </div>
    </div>

    <!-- 快速操作 -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">快速操作</h5>
                </div>
                <div class="card-body">
                    <div class="d-grid gap-2 d-md-flex">
                        <a href="{% url 'tickets:ticket_list_assigned' %}" class="btn btn-primary me-md-2">
                            <i class="bi bi-clipboard-check"></i> 查看我的工单
                        </a>
                        <a href="{% url 'reports' %}" class="btn btn-outline-secondary">
                            <i class="bi bi-bar-chart"></i> 查看绩效统计
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 已分配的工单 -->
    {% if assigned_tickets %}
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">已分配的工单</h5>
                    <span class="badge bg-primary">{{ assigned_tickets|length }}</span>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>工单号</th>
                                    <th>工单标题</th>
                                    <th>创建者</th>
                                    <th>分配时间</th>
                                    <th>当前状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for ticket in assigned_tickets %}
                                <tr>
                                    <td>
                                        {% if ticket.ticket_number %}
                                        <strong>{{ ticket.ticket_number }}</strong>
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ ticket.title }}</td>
                                    <td>{{ ticket.created_by.username }}</td>
                                    <td>{{ ticket.assigned_at|date:"Y-m-d H:i" }}</td>
                                    <td>
                                        <span class="badge bg-primary">
                                            {{ ticket.get_status_display }}
                                        </span>
                                    </td>
                                    <td>
                                        <a href="{% url 'tickets:ticket_detail' ticket.pk %}" class="btn btn-sm btn-outline-primary">
                                            查看详情
                                        </a>
                                        <a href="{% url 'tickets:ticket_testing' ticket.pk %}" class="btn btn-sm btn-warning">
                                            开始检测
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- 检测中的工单 -->
    {% if testing_tickets %}
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">检测中的工单</h5>
                    <span class="badge bg-warning">{{ testing_tickets|length }}</span>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>工单号</th>
                                    <th>工单标题</th>
                                    <th>创建者</th>
                                    <th>检测开始时间</th>
                                    <th>当前状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for ticket in testing_tickets %}
                                <tr>
                                    <td>
                                        {% if ticket.ticket_number %}
                                        <strong>{{ ticket.ticket_number }}</strong>
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ ticket.title }}</td>
                                    <td>{{ ticket.created_by.username }}</td>
                                    <td>{{ ticket.testing_at|date:"Y-m-d H:i" }}</td>
                                    <td>
                                        <span class="badge bg-warning">
                                            {{ ticket.get_status_display }}
                                        </span>
                                    </td>
                                    <td>
                                        <a href="{% url 'tickets:ticket_detail' ticket.pk %}" class="btn btn-sm btn-outline-primary">
                                            查看详情
                                        </a>
                                        <a href="{% url 'tickets:ticket_repairing' ticket.pk %}" class="btn btn-sm btn-info">
                                            开始维修
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- 维修中的工单 -->
    {% if repairing_tickets %}
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">维修中的工单</h5>
                    <span class="badge bg-info">{{ repairing_tickets|length }}</span>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>工单号</th>
                                    <th>工单标题</th>
                                    <th>创建者</th>
                                    <th>维修开始时间</th>
                                    <th>当前状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for ticket in repairing_tickets %}
                                <tr>
                                    <td>
                                        {% if ticket.ticket_number %}
                                        <strong>{{ ticket.ticket_number }}</strong>
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ ticket.title }}</td>
                                    <td>{{ ticket.created_by.username }}</td>
                                    <td>{{ ticket.repairing_at|date:"Y-m-d H:i" }}</td>
                                    <td>
                                        <span class="badge bg-info">
                                            {{ ticket.get_status_display }}
                                        </span>
                                    </td>
                                    <td>
                                        <a href="{% url 'tickets:ticket_detail' ticket.pk %}" class="btn btn-sm btn-outline-primary">
                                            查看详情
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- 工作统计和通知 -->
    <div class="row">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">今日工作</h5>
                </div>
                <div class="card-body">
                    <div class="alert alert-info">
                        <h6 class="alert-heading">今日任务</h6>
                        <p class="mb-0 small">您今天有 {{ ticket_stats.repairing }} 个维修中工单需要处理。</p>
                    </div>
                    <div class="alert alert-success">
                        <h6 class="alert-heading">工作效率</h6>
                        <p class="mb-0 small">本月已完成 {{ ticket_stats.completed }} 个工单。</p>
                    </div>
                    <div class="alert alert-warning">
                        <h6 class="alert-heading">检测状态</h6>
                        <p class="mb-0 small">当前有 {{ ticket_stats.testing }} 个工单正在检测中。</p>
                    </div>
                    <div class="alert alert-info">
                        <h6 class="alert-heading">维修状态</h6>
                        <p class="mb-0 small">当前有 {{ ticket_stats.repairing }} 个工单正在维修中。</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">系统通知</h5>
                </div>
                <div class="card-body">
                    {% if notifications %}
                        {% for notification in notifications %}
                        <div class="alert alert-{{ notification.get_priority_class }} mb-2">
                            <div class="d-flex justify-content-between align-items-start">
                                <div>
                                    <h6 class="alert-heading mb-1">{{ notification.title }}</h6>
                                    <p class="mb-0 small">{{ notification.content|truncatewords:15 }}</p>
                                </div>
                                <small class="text-muted">{{ notification.created_at|date:"m-d H:i" }}</small>
                            </div>
                        </div>
                        {% endfor %}
                    {% else %}
                        <div class="text-center text-muted py-3">
                            <i class="bi bi-bell" style="font-size: 2rem;"></i>
                            <p class="mt-2 mb-0">暂无通知</p>
                        </div>
                    {% endif %}
                    
                    {% if testing_tickets %}
                    <div class="alert alert-warning">
                        <h6 class="alert-heading">检测提醒</h6>
                        <p class="mb-0 small">您有 {{ testing_tickets|length }} 个工单正在检测中，请及时完成检测。</p>
                    </div>
                    {% endif %}
                    {% if repairing_tickets %}
                    <div class="alert alert-info">
                        <h6 class="alert-heading">维修提醒</h6>
                        <p class="mb-0 small">您有 {{ repairing_tickets|length }} 个工单正在维修中，请确保维修质量。</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
